package com.m4f.gaeweb.web.base.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;

import com.m4f.gaeweb.web.utils.StackTraceUtil;


public abstract class GaeBaseController {	
	private static final Logger LOGGER = Logger.getLogger(GaeBaseController.class.getName());
	
	private static final String SUCCESS_MESSAGES = "_success_messages";
	private static final String INFO_MESSAGES = "_info_messages";
	private static final String WARNING_MESSAGES = "_warning_messages";
	private static final String ERROR_MESSAGES = "_error_messages";
	
	private Map<String,Locale> localesMap;
	private List<Locale> avLocales;
	
	private HashMap<String, List<String>> messages = new HashMap<String, List<String>>();
	
	@Autowired
	protected ReloadableResourceBundleMessageSource messageSource;
	
	protected String getMessage(String msg, Object... args) {
		List<Object> variableParts = new ArrayList<Object>();
		if(args != null) {
			for(Object arg : args) {
				variableParts.add(arg);
			}
		}
		String message = "????" + msg + "????";
		try {
			message = this.messageSource.getMessage(msg,
					variableParts.toArray(), null);
		} catch(Exception e) {
			LOGGER.info(StackTraceUtil.getStackTrace(e));
		}
		return message;
	}
	
	protected boolean isSupportedMimeType(List<String> supportedMimeTypes, 
			String targetMimeType) {
		for(String mimeType : supportedMimeTypes) {
			if(mimeType.equals(targetMimeType)) {
				return true;
			}
		}
		return false;
	}
	
	protected String getRedirect(String url, Locale locale) {
		StringBuffer sb = new StringBuffer();
		sb.append("redirect:");
		if(locale != null) {
			sb.append("/");
			sb.append(locale.getLanguage());
		} 
		if(!url.startsWith("/")) {
			sb.append("/");
		}
		sb.append(url);
		return sb.toString();
	}
	
	protected void addSuccessMessage(String keyMessage) {
		List<String> messages = this.messages.get(SUCCESS_MESSAGES);
		if(messages == null) messages = new ArrayList<String>();
		messages.add(keyMessage);
		this.messages.put(SUCCESS_MESSAGES, messages);
		
	}
	
	protected void addInfoMessage(Model model, String keyMessage) {
		List<String> messages = this.messages.get(INFO_MESSAGES);
		if(messages == null) messages = new ArrayList<String>();
		messages.add(keyMessage);
		this.messages.put(INFO_MESSAGES, messages);
		
	}
	
	protected void addWarningMessage(Model model, String keyMessage) {
		List<String> messages = this.messages.get(WARNING_MESSAGES);
		if(messages == null) messages = new ArrayList<String>();
		messages.add(keyMessage);
		this.messages.put(WARNING_MESSAGES, messages);
	}
	
	protected void addErrorMessage(String keyMessage) {
		List<String> messages = this.messages.get(ERROR_MESSAGES);
		if(messages == null) messages = new ArrayList<String>();
		messages.add(keyMessage);
		this.messages.put(ERROR_MESSAGES, messages);
		
	}
	
	@ModelAttribute("messages")
	public HashMap<String, List<String>> getSuccessMessages() {
		HashMap<String, List<String>> messages = new HashMap<String, List<String>>(this.messages);
		this.messages.clear();
		return messages;
	}
}